package sjtu.pirate.httpclient;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.client.ClientProtocolException;
import sjtu.pirate.format.Content;
import sjtu.pirate.format.Form;
/**
 * @author wilbur.ma@qq.com
 * @date 2011.05.26
 * Client class which handles http communication.
 */
public class HTTPClient{
	private PirateClient client;
	private int connTimeout;
	private String encoding;
	private URI proxyURI;
	
	public HTTPClient() {
		this("UTF-8", 0, null);
	}
	
	public HTTPClient(URI proxyURI) {
		this(0, proxyURI);
	}
	
	public HTTPClient(int timeout, URI proxyURI) {
		this("UTF-8", timeout, proxyURI);
	}
	
	public HTTPClient(String encoding, int timeout, URI proxyURI) {
		this.encoding = encoding;
		this.connTimeout = timeout;
		this.proxyURI = proxyURI;
		this.client = new PirateClient(encoding, timeout, proxyURI);
	}
	
	public int sendRequest(Content content) {
		for (Form f : content.getFormList()) {
			try {
				client.sendRequest(f);
			} catch (URISyntaxException e) {
				e.printStackTrace();
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
			}
		}
		return 0;
	}

	public void shutdown() {
		client.shutdown();
	}
	
	public int getConnTimeout() {
		return connTimeout;
	}
	
	public String getEncoding() {
		return encoding;
	}

	public URI getProxyURI() {
		return proxyURI;
	}
}